package com.diaohaiwen.basic.websockt.obj.model;

import com.alibaba.fastjson.JSONObject;
import com.diaohaiwen.basic.websockt.obj.bo.FileBo;
import com.diaohaiwen.basic.websockt.obj.vo.NoticeVo;
import com.diaohaiwen.common.enums.NoticeEnum;
import com.diaohaiwen.common.obj.login.LoginUser;
import com.diaohaiwen.framework.authority.AuthorityUtil;
import com.diaohaiwen.framework.db.obj.BaseCModel;
import com.diaohaiwen.framework.web.util.RequestUtil;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 消息通知
 *
 * @author 刁海文
 * @since 2023/12/7 22:15
 */
@Data
@Table("NOTICE")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class Notice extends BaseCModel {

    /**
     * 发送者ID
     */
    private String senderId;
    /**
     * 发送者IP
     */
    private String senderIp;
    /**
     * 发送者名称
     */
    private String senderName;
    /**
     * 接受者ID
     */
    private String receiverId;
    /**
     * 接受者的名称
     */
    private String receiverName;
    /**
     * 发送内容
     */
    private String contents;
    /**
     * 是否为群组
     */
    @Column("IS_GROUP")
    private Boolean group;
    ;
    /**
     * 数据类型
     */
    private NoticeEnum noticeType;

    public FileBo file() {
        return JSONObject.parseObject(this.contents, FileBo.class);
    }

    public NoticeVo toVO() {
        NoticeVo build = NoticeVo.builder()
                .noticeId(super.getId())
                .senderId(this.senderId)
                .senderIp(this.senderIp)
                .senderName(this.senderName)
                .noticeType(this.noticeType)
                .build();

        switch (noticeType) {
            case FILE -> build.setFile(file().toVo());
            case TEXT -> build.setTexts(contents.split("\n"));
        }

        return build;
    }

    public static Notice build() {
        Notice notice = new Notice();
        LoginUser user = AuthorityUtil.getLoginUser();
        notice.senderId = user.getLoginId();
        notice.senderName = user.getName();
        notice.senderIp = RequestUtil.ip();
        return notice;
    }
}
